
var Comments = (function(){

	function handleElements() {
		
	}

	function handleGrid() {

		var boolRender = function(v, r, i) {
			var color = v == '1' ? 'text-success' : 'text-danger';
			return '<i class="icon-circle '+color+'"></i>';
		};

		$('#grid').datagrid({
			url: siteURL('articles/categories/load_data'),
			/*idField:'c_id',
			treeField:'c_caption_en',*/
			title: 'Article Category List',
			height: 400,
			rownumbers: true,
			pagination: true,
			pagePosition: 'top',
			autoRowHeight: false,
			checkbox: true,
			singleSelect: false,
			columns: [[
				// {field: 'c_id', title: 'ID', width: 40, align: 'center'},
				{field: 'ck', checkbox: true},
				{field: 'ca_name', title: 'Name', width: 200, sortable: true},
				{field: 'ca_desc', title: 'Desc.', width: 200, sortable: true},
				{field: 'ca_active', title: 'Active', width: 50, align: 'center', formatter: boolRender, sortable: true},
				{field: 'ca_order', title: 'Order', width: 50, align: 'center', sortable: true}
			]]
		});

		var pager = $('#grid').datagrid('getPager');
		pager.pagination({
			buttons: [
				{text: '<i class="icon-plus"></i> Add',  handler: addRecord},
				{text: '<i class="icon-pencil"></i> Edit', handler: editRecord},
				{text: '<i class="icon-trash"></i> Delete', handler: deleteRecord}
			]
		});

	}

	function handleForm() {
		/*$('input[name=c_parent_id]').combobox({
			width: 231,
			height: 32,
			url: siteURL('articles/categories/load_parent'),
			valueField: 'id',
			textField: 'text',
			mode: 'remote'
		});

		$('input[name=c_is_active]').combobox({
			width: 231,
			height: 32,
			panelHeight: 100,
			data: [
				{id: '1', text: 'Yes'},
				{id: '0', text: 'No'}
			],
			valueField: 'id',
			textField: 'text',
			mode: 'local'
		});*/
	}

	function handleWindow() {
		$('#win').show();
		$('#win').dialog({
			modal: true,
			width: 420,
			title: 'Comments Editor',
			buttons: [
				{
					text: 'Submit',
					handler: function() {
						$('#form').form('submit', {
							url: siteURL('articles/categories/save_data'),
							success: function() {
								$('#win').dialog('close');
								$('#grid').datagrid('reload');
							}
						});
					}
				},
				{
					text: 'Close',
					handler: function() {
						$('#win').dialog('close');
					}
				}
			]
		});
		$('#win').dialog('close');
	}

	function addRecord() {
		$('#win').dialog('open');
		$('#form').form('clear').form('reset');
	}

	function editRecord() {
		var record = $('#grid').datagrid('getSelected');
		if ( ! record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}

		$('#win').dialog('open');
		$('#form').form('load', record);
	}

	function deleteRecord() {
		var record = $('#grid').datagrid('getSelected');
		if ( ! record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}

		$.messager.confirm('Confirm', 'Delete selected record?', function(r){
			if (r) {
				$.ajax({
					url: siteURL('articles/categories/delete_data'),
					type: 'post',
					dataType: 'json',
					data: record
				})
				.done(function(){
					$('#grid').datagrid('reload');
				});	
			}
		});
	}

	function handleSearch() {
		$('#omnisearch').on('keypress', function(e){
			if (e.keyCode === 13) {
				var query = $(this).val();
				$('#grid').datagrid('load', {
					query: query
				});
			}
		});
	}

	return {
		init: function() {
			handleElements();
			handleGrid();
			handleForm();
			handleWindow();
			handleSearch();
		}
	};

}(jQuery));

$(document).ready(Comments.init);